package com.hezhou.jenkins.my_jenkins_test.json;

import lombok.Data;

/**
 * ---------------------------
 * (HeZhouHeZhouResult) 统一返回json类
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/4/21
 * @Version: [1.0.1]
 * ---------------------------
 */
@Data
public class HeZhouResult<T> {
    /**
     * 返回信息
     */
    private String message;
    /**
     * 状态码
     */
    private int retCode;
    /**
     * 返回参数
     */
    private T data;

    private HeZhouResult(T data) {
        this.retCode = 0;
        this.message = "成功";
        this.data = data;
    }
    private HeZhouResult(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.retCode = cm.getRetCode();
        this.message = cm.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> HeZhouResult<T> success(T data){
        return new HeZhouResult<T>(data);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> HeZhouResult<T> success(){
        return (HeZhouResult<T>) success("");
    }

    /**
     * 失败时候的调用
     * @return
     */
    public static <T> HeZhouResult<T> error(CodeMsg cm){
        return new HeZhouResult<T>(cm);
    }

    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> HeZhouResult<T> error(CodeMsg cm,String msg) {
        cm.setMessage(cm.getMessage() + "--" + msg);
        return new HeZhouResult<T>(cm);
    }

}
